﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Minecraft_Xenon
{
    /// <summary>
    /// NOTICE:呵呵体检(取什么名字不好...)
    /// </summary>
    public class HeheExamination
    {
        private string McPath;
        private int Score = 0;
        private int State = 0;

        private RichTextBox TextBox;
        private Label TextLabel;
        private LinkLabel Button;

        public void SetConfig(string mcpath, RichTextBox textBox, Label label, LinkLabel linklabel)
        {
            McPath = mcpath;
            TextBox = textBox;
            TextLabel = label;
            Button = linklabel;
        }

        public int GetScore()
        {
            return Score;
        }

        public int GetState()
        {
            return State;
        }

        public void StartChecking()
        {
            TextBox.Clear();

            AppendLog("准备开始检测..." + Environment.NewLine);

            if (File.Exists(McPath + @"\bin\minecraft.jar") == true)
            {
                Score += 100;
                AppendLog("检测到 minecraft.jar，获得基础分数 +100！ " + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }

            if (File.Exists(McPath + @"\optionsof.txt") == true)
            {
                Score += 20;
                AppendLog("检测到已安装 OptFine, +20" + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }

            if (System.IO.Directory.Exists(McPath + @"\mods") == true)
            {
                Score += 10;
                AppendLog("检测到支持Mod, +10" + Environment.NewLine);
                TextLabel.Text = Score.ToString();

                Score += System.IO.Directory.GetFiles(McPath + @"\mods").Length * 10;
                AppendLog("检测到已安装Mod, " + "+" + (System.IO.Directory.GetFiles(McPath + @"\mods").Length * 10) + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }

            if (System.IO.Directory.Exists(McPath + @"\crash-reports") == true)
            {
                Score -= System.IO.Directory.GetFiles(McPath + @"\crash-reports").Length * 5;
                AppendLog("检测到游戏曾经发生过错误, -" + System.IO.Directory.GetFiles(McPath + @"\crash-reports").Length * 5 + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }

            if (File.Exists(McPath + @"\mcpatcher.xml") == true)
            {
                Score += 10;
                AppendLog("检测到高清支持, +10" + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }



            if (File.Exists(McPath + @"\bin\natives\jinput-dx8.dll") == true ||
                File.Exists(McPath + @"\bin\natives\jinput-dx8_64.dll") == true ||
                File.Exists(McPath + @"\bin\natives\jinput-raw.dll") == true ||
                File.Exists(McPath + @"\bin\natives\jinput-raw_64.dll") == true ||
                File.Exists(McPath + @"\bin\natives\lwjgl.dll") == true ||
                File.Exists(McPath + @"\bin\natives\lwjgl64.dll") == true ||
                File.Exists(McPath + @"\bin\natives\OpenAL32.dll") == true ||
                File.Exists(McPath + @"\bin\natives\OpenAL64.dll") == true)
            {
                Score += 50;
                AppendLog("检测到完整的图形支持类库, +50" + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }


            if (File.Exists(McPath + @"\resources\streaming\13.ogg") == true)
            {
                Score += 40;
                AppendLog("检测到有效的资源文件, +40" + Environment.NewLine);
                TextLabel.Text = Score.ToString();
            }

            Score -= Directory.GetFiles(McPath, "*.log").Length * 2;
            AppendLog("检测垃圾文件, -" + System.IO.Directory.GetFiles(McPath, "*.log").Length * 2 + Environment.NewLine);

            AppendLog("检测到最后，您的得分是：" + Score + Environment.NewLine);
            TextLabel.Text = Score.ToString();

            if (Score == 150)
            {
                AppendLog("您的 Minecraft 属于正常水平，一般是没经过任何修改的版本");
            }

            if (Score > 150 && Score <= 200)
            {
                AppendLog("您的 Minecraft 比较丰满，可玩性较高");
            }

            if (Score > 200)
            {
                AppendLog("您的 Minecraft 比较丰富，您可以享受Mod您带来的乐趣！");
            }

            if (Score > 300)
            {
                AppendLog("您的 Minecraft 很丰富，您可以尽情享受游戏给您带来的乐趣！");
            }

            State = 1;
            Button.Text = "一秒优化";
        }


        public void OptiMinecraft()
        {
            Score = 0;

            if (State == 1)
            {
                string[] crashlog = Directory.GetFiles(McPath, "*.log");

                foreach (string strFile in crashlog)
                {
                    File.Delete(strFile);
                    AppendLog("已删除：" + strFile + Environment.NewLine);
                }

                if (Directory.Exists(McPath + @"\stats") == true)
                {
                    Directory.Delete(McPath + @"\stats", true);
                    AppendLog("已删除历史记录：" + McPath + @"\stats" + Environment.NewLine);
                }

                if (File.Exists(McPath + @"\lastlogin") == true)
                {
                    File.Delete(McPath + @"\lastlogin");
                    AppendLog("已删除LASTLOGIN：" + McPath + @"\lastlogin" + Environment.NewLine);
                }

                if (File.Exists(McPath + @"\ModLoader.txt") == true)
                {
                    File.Delete(McPath + @"\ModLoader.txt");
                    AppendLog("已删除MODLoader日志：" + McPath + @"\ModLoader.txt" + Environment.NewLine);
                }

                AppendLog("优化完成！如游戏无法启动请汇报bug…");

                State = 2;
                Button.Text = "深度优化"; 
            }
            else if (State == 2)
            {

                if (File.Exists(McPath + @"\TooManyItems.txt") == true)
                {
                    string TMI = File.ReadAllText(McPath + @"\TooManyItems.txt");
                    TMI = TMI.Replace("{0} {1} {2} {3}", "{1}:{3} {2}");
                    File.WriteAllText(McPath + @"\TooManyItems.txt", TMI);
                    AppendLog("已修改TMI为联机模式。" + Environment.NewLine);
                }

                if (Directory.Exists(McPath + @"\crash-reports") == true)
                {
                    Directory.Delete(McPath + @"\crash-reports", true);
                    AppendLog("已删除错误日志。" + Environment.NewLine);
                }

                AppendLog("深度优化完成！如游戏无法启动请汇报bug…");

                State = 0;
                Button.Text = "一秒体检";
            }
        }

        private void AppendLog(string text)
        {
            TextBox.AppendText(text);
            TextBox.ScrollToCaret();
        }
    }
}
